$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'remittance/remittance/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '商户ID', name: 'sellerId', index: 'sellerId', width: 50, align: 'center'},
            {label: '收款人姓名', name: 'receiverName', index: 'receiver_name', width: 80, align: 'center'},
            {
                label: '银行卡所属支行',
                name: 'receiverAccountBranch',
                index: 'receiver_account_branch',
                width: 80,
                align: 'center'
            },
            {
                label: '银行卡号', name: 'receiverAccount', index: 'receiver_account', width: 80, align: 'center',
                formatter: function (value, options, row) {
                    return value.replace(/^(\d{4})\d{8}(\d+)/, "$1*******$2")
                }
            },
            {label: '所属门店', name: 'sellerName', width: 80, align: 'center'},
            {
                label: '银行账户属性', name: 'receiverBankAccountPro', width: 80, align: 'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '对公' : '对私';
                }
            },
            {
                label: '证件类型', name: 'credentialType', width: 80, align: 'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '身份证' : '';
                }
            },
            {label: '身份证号码', name: 'receiverCredential', width: 80, align: 'center'},
        ],
        viewrecords: true,
        height: 700,
        rowNum: 25,
        rowList: [25, 50, 100],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $.get(baseURL + "remittance/remittance/bankInfos", function (r) {
                vm.bankInfos = r;
            });
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        remittance: {},
        q: {
            receiverName: '',
            sellerId: ''
        },
        sellerNames: [],//所属门店集合
        options: [],
        list: [],
        loading: false,
        states: [],
        bankInfos: []
    },
    mounted() {
        $.get(baseURL + "remittance/remittance/getsellerNames", function (r) {
            vm.states = r;
            vm.list = vm.states.map(item => {
                return {value: item.seller_id, label: item.seller_name};
            });
        });

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.remittance = {
                receiverBankAccountPro: 0,
                credentialType: 1
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            if (!vm.remittance.sellerId || !vm.remittance.receiverAccount || !vm.remittance.receiverName || !vm.remittance.receiverAccountBranch) {
                alert('必填项不能为空!')
                return
            }
            //对私身份证必填
            if (vm.remittance.receiverBankAccountPro == 0 && !vm.remittance.receiverCredential) {
                alert('对私身份证不能为空!')
                return
            }
            //对公銀行必填
            if (vm.remittance.receiverBankAccountPro == 1 && !vm.remittance.receiverGateId) {
                alert('收款方账户的发卡行不能为空!')
                return
            }
            //校验身份证
            if (vm.remittance.receiverCredential) {
                var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                if (reg.test(vm.remittance.receiverCredential) == false) {
                    alert("身份证输入不合法!");
                    return;
                }
            }
            vm.remittance.receiverAccount = vm.remittance.receiverAccount.trim();
            vm.remittance.receiverName = vm.remittance.receiverName.trim()
            vm.remittance.receiverAccountBranch = vm.remittance.receiverAccountBranch.trim()
            vm.remittance.receiverCredential = vm.remittance.receiverCredential ? vm.remittance.receiverCredential.trim() : ''
            var url = vm.remittance.id == null ? "remittance/remittance/save" : "remittance/remittance/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.remittance),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.q.sellerId = ''
                            vm.reload();
                        });
                    } else {
                        vm.q.sellerId = ''
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "remittance/remittance/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "remittance/remittance/info/" + id, function (r) {
                vm.remittance = r.remittance;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'receiverName': vm.q.receiverName.trim(),
                    'sellerId': vm.q.sellerId
                },
                page: 1
            }).trigger("reloadGrid");
        },
        reset() {
            vm.q.receiverName = ''
            vm.q.sellerId = ''
            vm.reload();
        },
        remoteMethod(query) {
            if (query !== '') {
                vm.loading = true;
                setTimeout(() => {
                    vm.loading = false;
                    vm.options = vm.list.filter(item => {
                        return item.label.indexOf(query) > -1;
                    });
                }, 200);
            } else {
                vm.options = [];
            }
        }

    }
});